import {
  getAnnounce,
  getNotice,
  getNoticeNum,
  getSignSuccess,
  getMsgUrl,
  getSignAll,
  getAddNotice,
} from '@/services/tipnotice';
import { message } from 'antd';
import router from 'umi/router';

export default {
  state: {
    data: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    },
    countData: 0,
    activeKey: '1',
  },
  effects: {
    // 查询公告
    *fetchAnnounce({ payload }, { call, put }) {
      const response = yield call(getAnnounce, payload);
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getAnnounce',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message);
      }
    },
    // 查询消息
    *fetchNotice({ payload }, { call, put }) {
      const response = yield call(getNotice, payload);
      const { currentPage, pageSize } = payload;
      if (response.success) {
        yield put({
          type: 'getAnnounce',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message);
      }
    },
    *fetchNoticeNum({ payload }, { call, put }) {
      const response = yield call(getNoticeNum, payload);
      if (response.success) {
        yield put({
          type: 'getNoticeNum',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },
    *fetchSignSuccess({ payload }, { call, put, select }) {
      const response = yield call(getSignSuccess, payload);
      // 个数
      if (response.success) {
        const resNum = yield call(getNoticeNum, {});
        if (resNum.success) {
          yield put({
            type: 'getNoticeNum',
            payload: resNum.data,
          });
          message.success('处理成功!');
        } else {
          message.warning(resNum.message);
        }
      } else {
        message.warning(response.message);
      }
      // getlist
      const { current, pageSize } = yield select(state => state.tipnotice.data.pagination);
      const { activeKey } = yield select(state => state.tipnotice);
      const resNotice = yield call(getNotice, { current, pageSize, sendType: activeKey });
      if (resNotice.success) {
        yield put({
          type: 'getAnnounce',
          payload: {
            data: resNotice.data,
            currentPage: current,
            pageSize,
          },
        });
      } else {
        message.warning(resNotice.message);
      }
    },
    *fetchMsgUrl({ payload }, { call }) {
      const response = yield call(getMsgUrl, payload);
      if (response.success && response.data) {
        if (response.data.reqParamType === 1) {
          // 详情
          router.push(response.data.msgUrl);
        } else {
          // 列表
          const routerParams = {
            pathname: response.data.msgUrl,
            queryParam: JSON.parse(response.data.dataParam),
            query: {
              isMenuLink: false,
              activeKey: response.data.dataParam.curStep,
            },
          };
          router.push(routerParams);
        }
      } else {
        message.warning(response.message);
      }
    },
    *fetchSignAll({ payload }, { call, put, select }) {
      const response = yield call(getSignAll, payload);

      if (response.success) {
        const resNum = yield call(getNoticeNum, {});
        if (resNum.success) {
          yield put({
            type: 'getNoticeNum',
            payload: resNum.data,
          });
          message.success(resNum.message || '全部处理成功!');
        } else {
          message.warning(resNum.message);
        }
      }
      // getlist
      const { current, pageSize } = yield select(state => state.tipnotice.data.pagination);
      const { activeKey } = yield select(state => state.tipnotice);
      const resNotice = yield call(getNotice, { current, pageSize, sendType: activeKey });
      if (resNotice.success) {
        yield put({
          type: 'getAnnounce',
          payload: {
            data: resNotice.data,
            currentPage: current,
            pageSize,
          },
        });
      } else {
        message.warning(resNotice.message);
      }
    },
    *fetchAddNotice({ payload }, { call }) {
      const response = yield call(getAddNotice, payload);
      if (response.success) {
        return response;
        // message.success(`${response.message || '新增成功！'}`);
      }
      return message.warning(response.message);
    },
  },

  reducers: {
    // 查询用户基本信息
    getAnnounce(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    getNoticeNum(state, { payload }) {
      // console.log('111', list);
      return {
        ...state,
        countData: payload,
      };
    },
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
